package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.entity.*;
import com.lzy.liujing.restaurant.service.GoodsCategoryService;
import com.lzy.liujing.restaurant.service.GoodsService;
import com.lzy.liujing.restaurant.service.OrderDetailService;
import com.lzy.liujing.restaurant.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController{
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    OrderDetailService orderDetailService;

    @GetMapping("/current/{current}/size/{size}")
    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<Goods> brandPageItem = goodsService.queryPage(current, size);
        List<Goods> goodsList = brandPageItem.getItems();

        List<GoodsDisplay> goodsDisplayList = new ArrayList<>();
        for (Goods goods : goodsList) {
            // 调用GoodsCategory服务获取对应的CategoryName
            GoodsCategory category = goodsCategoryService.getById(goods.getCategoryId());
            OrderDetail byId = orderDetailService.getById(goods.getGoodsId());
            // 创建一个新对象，用于展示，包含CategoryName
            GoodsDisplay goodsDisplay = new GoodsDisplay(
                    goods.getGoodsId(),
                    goods.getGoodsName(),
                    category.getCategoryName(), // 使用CategoryName替换category_id
                    goods.getImgUrl(),
                    goods.getPrice(),
                    goods.getCost(),
                    goods.getDiscount(),
                    goods.getSoldCount(),
                    goods.getStoreCount(),
                    goods.getDescription(),
                    goods.getTypeState(),
                    goods.getSoldState()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<GoodsDisplay> displayPageItem = new PageItem<GoodsDisplay>(brandPageItem.getPageNumber(), brandPageItem.getPageSize(), goodsDisplayList);

        Result result = new Result(displayPageItem, "查询成功");
        return result;

    }

    @PostMapping("/current/{current}/size/{size}")
    public Result getCondPage(@PathVariable Integer current,
                              @PathVariable Integer size,
                              @RequestBody Goods query) {
        //执行查询
        PageItem<Goods> item = goodsService.queryConditionPage(query,current,size);
        List<Goods> goodsList = item.getItems();

        List<GoodsDisplay> goodsDisplayList = new ArrayList<>();
        for (Goods goods : goodsList) {
            // 调用GoodsCategory服务获取对应的CategoryName
            GoodsCategory category = goodsCategoryService.getById(goods.getCategoryId());
            OrderDetail byId = orderDetailService.getById(goods.getGoodsId());
            // 创建一个新对象，用于展示，包含CategoryName
            GoodsDisplay goodsDisplay = new GoodsDisplay(
                    goods.getGoodsId(),
                    goods.getGoodsName(),
                    category.getCategoryName(), // 使用CategoryName替换category_id
                    goods.getImgUrl(),
                    goods.getPrice(),
                    goods.getCost(),
                    goods.getDiscount(),
                    goods.getSoldCount(),
                    goods.getStoreCount(),
                    goods.getDescription(),
                    goods.getTypeState(),
                    goods.getSoldState()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<GoodsDisplay> displayPageItem = new PageItem<GoodsDisplay>(item.getPageNumber(), item.getPageSize(), goodsDisplayList);

        Result result = new Result(displayPageItem, "查询成功");
        return result;

    }

    @PostMapping("/good/selectAll")
    public Result getCond(@RequestBody Goods query) {
        //执行查询
        List<Goods> selectedAll = goodsService.getSelectedAll(query);
        List<GoodsDisplay> goodsDisplayList = new ArrayList<>();
        for (Goods goods : selectedAll) {
            // 调用GoodsCategory服务获取对应的CategoryName
            GoodsCategory category = goodsCategoryService.getById(goods.getCategoryId());
            // 创建一个新对象，用于展示，包含CategoryName
            GoodsDisplay goodsDisplay = new GoodsDisplay(
                    goods.getGoodsId(),
                    goods.getGoodsName(),
                    category.getCategoryName(), // 使用CategoryName替换category_id
                    goods.getImgUrl(),
                    goods.getPrice(),
                    goods.getCost(),
                    goods.getDiscount(),
                    goods.getSoldCount(),
                    goods.getStoreCount(),
                    goods.getDescription(),
                    goods.getTypeState(),
                    goods.getSoldState()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<GoodsDisplay> displayPageItem = new PageItem<GoodsDisplay>(goodsDisplayList);

        return new Result(displayPageItem, "查询成功");

    }

    @DeleteMapping("/goods/{id}")
    public Result delete(@PathVariable Integer id, HttpServletRequest request) {
        Integer accountId = (Integer) request.getAttribute("accountId");
        boolean flag = goodsService.delete(id);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }

    @GetMapping("/good/{id}")
    public Result getOne(@PathVariable Integer id) {
        Goods byId = goodsService.getById(id);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }

    @DeleteMapping("/good/{ids}")
    public Result deleteMultiRec(@PathVariable int[] ids) {
        boolean flag = goodsService.deleteSome(ids);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }
    @PostMapping("/good")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody Goods brand) {

        System.out.println(brand);
        boolean insertBrand = goodsService.insert(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @RequestMapping("/good/update")
    // ResponseBody 将json字符串 转换为java 列表
    public Result updateRed(@RequestBody Goods brand) {

        boolean updateBrand = goodsService.update(brand);
        if(updateBrand){
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }

    @GetMapping("/good/getAll")
    public Result getAll() {
        List<Goods> item = goodsService.getAll();
        List<GoodsDisplay> goodsDisplayList = new ArrayList<>();
        for (Goods goods : item) {
            // 调用GoodsCategory服务获取对应的CategoryName
            GoodsCategory category = goodsCategoryService.getById(goods.getCategoryId());
            OrderDetail byId = orderDetailService.getById(goods.getGoodsId());
            // 创建一个新对象，用于展示，包含CategoryName
            GoodsDisplay goodsDisplay = new GoodsDisplay(
                    goods.getGoodsId(),
                    goods.getGoodsName(),
                    category.getCategoryName(), // 使用CategoryName替换category_id
                    goods.getImgUrl(),
                    goods.getPrice(),
                    goods.getCost(),
                    goods.getDiscount(),
                    goods.getSoldCount(),
                    goods.getStoreCount(),
                    goods.getDescription(),
                    goods.getTypeState(),
                    goods.getSoldState()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<GoodsDisplay> displayPageItem = new PageItem<GoodsDisplay>(goodsDisplayList);

        return new Result(displayPageItem, "查询成功");

    }
}
